<template>
  <div class="warpper-box sendSalary">
    <enterprise-select v-if="userInfo.type == 4" ref="enterprise"
      @enterpriseChange="enterpriseChange"></enterprise-select>
    <a-card style="margin-top: 10px;">
      <a-tabs v-model="params.invoiceStatus" @change="tabsChange">
        <a-tab-pane key="0" tab="待开票"></a-tab-pane>
        <a-tab-pane key="1" tab="申请中"></a-tab-pane>
        <a-tab-pane key="2" tab="已开票"></a-tab-pane>
        <a-tab-pane key="3" tab="已拒绝"></a-tab-pane>
      </a-tabs>
      <div v-if="params.invoiceStatus == '0'">
        <a-form layout="inline">
          <a-form-item label="订单名称">
            <a-input placeholder="请输入订单名称" v-model="params.orderName"></a-input>
          </a-form-item>
          <a-form-item label="订单编号">
            <a-input placeholder="请输入订单编号" v-model="params.orderId"></a-input>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" icon="search" @click="loadTable(true)">搜索</a-button>
            <a-button type="primary" icon="setting" @click="reset()">重置</a-button>
            <a-button type="primary" icon="plus" @click="mergeInvoice"
              :disabled="selectedRowKeys.length == 0">合并开票</a-button>
          </a-form-item>
        </a-form>
        <a-table :columns="table.columns" :data-source="table.loadData" :pagination="table.pagination"
          @change="handleTableChange" rowKey="orderId"
          :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }">
        </a-table>
      </div>
      <div v-else>
        <a-form layout="inline">
          <a-form-item label="申请时间">
            <a-range-picker @change="rangeChange" valueFormat="YYYY-MM-DD" v-model="createDate" />
          </a-form-item>
          <a-form-item label="发票编号">
            <a-input placeholder="请输入发票编号" v-model="params.invoiceId"></a-input>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" icon="search" @click="loadTable(true)">搜索</a-button>
            <a-button type="primary" icon="setting" @click="reset()">重置</a-button>
          </a-form-item>
        </a-form>
        <a-table :columns="table.columns" :data-source="table.loadData" :pagination="table.pagination"
          @change="handleTableChange" rowKey="invoiceId">
          <!-- 操作栏-->
          <span slot="action" slot-scope="text, record" v-if="params.invoiceStatus !== '0'">
            <!-- 编辑模块 | 字段 -->
            <a @click="$refs.addInfo.add(record.invoiceId, enterpriseId, params.invoiceStatus)">详情</a>
          </span>
        </a-table>
      </div>
    </a-card>
    <add-info ref="addInfo" @on-ok="addLoad"></add-info>
  </div>
</template>

<script>
import store from '@/store'
import AddInfo from "./invoiceApply_add.vue"
import EnterpriseSelect from "@/components/human/enterpriseSelect.vue"
import {
  OPTION_BLOCK,
  OPTION_INLINE
} from "../base_form"
import {
  getWaitTablePage,
  getInvoiceTablePage
} from "@/api/human/finance"
import {
  checkRepeat
} from "@/utils/util"
import { mapGetters, mapActions } from 'vuex'
export default {
  name: 'invoiceHead',
  components: {
    AddInfo,
    EnterpriseSelect
  },
  data() {
    return {
      createDate: [],
      params: {
        invoiceStatus: '0',
        orderId: "",
        orderName: "",
        invoiceId: "",
        startInvoiceApplyTime: undefined,
        endInvoiceApplyTime: undefined,
      },
      table: {
        columns: [],
        loadData: [],
        pagination: {
          current: 1,
          pageSize: 10,
          total: 0,
          showSizeChanger: true,
					showQuickJumper: true,
          pageSizeOptions: ["10", "20", "50", "100"],
          showTotal: total => `共有 ${total} 条数据`,
        }
      },
      pagination: { current: 1, pageSize: 10 },

      enterpriseId: "", // 企业id
      enterpriseName: "", // 企业名称
      enterpriseData: [], // 客户列表

      selectedRowKeys: [], // 选择的table数据id
      selectedRows: [], // 选择的table数据
    }
  },
  filters: {
    filterMore(value, num) {
      if (!value) return ''
      if (value.length > num) {
        return value.slice(0, num) + '...'
      }
      return value
    }
  },
  mounted() {
    window.addEventListener('keyup', this.keyDown);
    if (this.userInfo.type == 4) {
      this.$refs.enterprise.getEnterpriseData();
    } else {
      this.enterpriseId = this.userInfo.relationId
      this.loadTable(true);
    }
  },
  destroyed() {
    window.removeEventListener('keyup', this.keyDown);
  },
  beforeCreate() {
    this.events_form = this.$form.createForm(this, {
      name: 'events_form'
    })
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  methods: {
    // table复选框选择
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows
    },
    tabsChange(e) {
      if (e == '0') {
        this.params.orderName = '';
        this.params.orderId = '';
      } else {
        this.params.startInvoiceApplyTime = undefined;
        this.params.endInvoiceApplyTime = undefined;
        this.params.invoiceId = "";
      }
      this.table.loadData = [];
      this.loadTable(true);
    },
    // 获取列表
    async loadTable(flag) {
      let params = this.params;
      this.selectedRowKeys = [];
      this.selectedRows = [];
      if (flag) {
        this.table.pagination.current = 1;
        this.table.pagination.pageSize = 10;
      }
      if (this.params.invoiceStatus == '0') {
        this.table.columns = [
          {
            title: '订单',
            dataIndex: 'orderName'
          },
          {
            title: '订单编号',
            dataIndex: 'orderId'
          },
          {
            title: '订单服务商',
            dataIndex: 'parkName',
          },
          {
            title: '账单编号',
            dataIndex: 'billId',
          },
          {
            title: '订单类型',
            dataIndex: 'orderTypeDesc',
          },
          {
            title: '订单金额',
            dataIndex: 'orderAmount',
          },
          {
            title: '服务费金额',
            dataIndex: 'serviceFee',
          },
          {
            title: '可开票金额',
            dataIndex: 'invoiceAmount',
          }
        ]
      } else {
        this.table.columns = [
          {
            title: '发票申请编号',
            dataIndex: 'invoiceId'
          },
          // {
          //   title: '发票类型',
          //   dataIndex: 'invoiceTypeDesc'
          // },
          {
            title: '订单服务商',
            dataIndex: 'parkName',
          },
          // {
          //   title: '开票类目',
          //   dataIndex: 'invoiceCategoryDesc',
          // },
          {
            title: '订单数量',
            dataIndex: 'orderCount',
          },
          {
            title: '订单金额',
            dataIndex: 'invoiceOrderAmount',
          },
          {
            title: '服务费金额',
            dataIndex: 'invoiceServiceFee',
          },
          {
            title: '开票金额',
            dataIndex: 'invoiceAmount',
          },
          {
            title: this.params.invoiceStatus == '1' ? '开票申请时间' : "开票审核时间",
            dataIndex: this.params.invoiceStatus == '1' ? 'invoiceApplyTime' : 'invoiceCheckTime',
          },
          {
            title: '操作',
            dataIndex: 'action',
            fixed: 'right',
            scopedSlots: { customRender: 'action' }
          },
        ]
      }
      params.enterpriseId = this.enterpriseId;
      params.pageNum = this.table.pagination.current;
      params.pageSize = this.table.pagination.pageSize;
      const { code, data } = this.params.invoiceStatus == '0' ? await getWaitTablePage(params) : await getInvoiceTablePage(params);
      if (code === '1') {
        this.table.loadData = data.content;
        this.table.pagination.total = data.total || 0;
      }
    },
    handleTableChange(pagination) {
      this.table.pagination.current = pagination.current;
      this.table.pagination.pageSize = pagination.pageSize;
      this.loadTable(false);
    },
    rangeChange(e) {
      if (e || e.length > 0) {
        this.params.startInvoiceApplyTime = e[0];
        this.params.endInvoiceApplyTime = e[1];
      }
    },
    // 合并开票
    mergeInvoice() {
      if (checkRepeat(this.selectedRows, 'parkId')) {
        this.$refs.addInfo.add(this.selectedRowKeys, this.enterpriseId, this.params.invoiceStatus);
      } else {
        this.$message.warning("请选择相同的服务商开票!")
      }
    },
    // 企业选择回调
    enterpriseChange(value) {
      this.enterpriseId = value;
      this.loadTable(true);
    },
    addLoad() {
      this.loadTable(true);
    },
    reset() {
      this.params.startInvoiceApplyTime = undefined;
      this.params.endInvoiceApplyTime = undefined;
      this.params.invoiceId = "";
      this.params.orderName = "";
      this.params.orderId = "";
      this.createDate = [];
      this.loadTable(true);
    }
  }
}
</script>
<style lang="less"></style>